package com.atguigu.juc.t10;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class Bread {
    public static void main(String[] args) {
        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        new Thread(() -> {
            int i = 1;
            while (true){
                String bread = "面包" + i;

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                System.out.println(Thread.currentThread().getName() + "开始做面包");
                try {
                    arrayBlockingQueue.put(bread);
                    System.out.println(Thread.currentThread().getName() + "把面包放在货架上");
                    System.out.println("**********" + arrayBlockingQueue);
                    i++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "面包师傅").start();

        new Thread(() -> {
           while (true){
               System.out.println(Thread.currentThread().getName() + "开始吃面包");
               try {
                   String take = arrayBlockingQueue.take();
                   TimeUnit.SECONDS.sleep(2);
                   System.out.println(Thread.currentThread().getName() + "从货架上取出" + take + "吃完后面包货架为");
                   System.out.println("----------" + arrayBlockingQueue);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        }, "吃货").start();
    }
}
